//对axios进行二次封装
import axios from 'axios'

import { Toast } from 'vant';

//实例化axios
var service = axios.create({
        timeout: 10 * 1000,
        baseURL: '/api'
})

//设置拦截器( 请求拦截器 )
service.interceptors.request.use((config) => {
        //统一携带请求头
        config.headers['Authorization'] = 'Bearer' + ' ' + localStorage.getItem('token');
        //显示loading
        Toast.loading({ message: '加载中...', duration: 0 });

        return config;
}, (error) => {
        return Promise.reject(error);
})

//设置拦截器( 响应拦截器 )
service.interceptors.response.use((res) => {
        //隐藏loading
        Toast.clear();

        return res;
}, (error) => {
        //隐藏loading
        Toast.clear();

        if (error.response.status == 401) {
                alert('身份认证失败,登陆过期!')
        }
        else if (error.response.status == 404) {
                alert('访问路径错误!')
        }
        else if (error.response.status == 500) {
                alert('服务器内部错误')
        }
        return Promise.reject(error);
})

export default service;

